package com.scores.controller;

import com.scores.pojo.Student;
import com.scores.pojo.Teacher;
import com.scores.utils.Results;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController // @RestController = @Controller + @ResponseBody
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/loginOut")
    public Results<String> loginOut(HttpSession session) {
        session.invalidate();
        return new Results<>("0", "注销成功");
    }

    @RequestMapping("/getUserName")
    public Results<String> getUserName(HttpSession session) {
        Teacher teacher = session.getAttribute("loginUser") instanceof Teacher ? ((Teacher) session.getAttribute("loginUser")) : null;
//        Teacher teacher = (Teacher) session.getAttribute("loginUser");
        if (teacher == null) {
            Student student = (Student) session.getAttribute("loginUser");
            if (student == null) {
                return new Results<>("1", "未登录");
            }else{
                return new Results<>("0", "success", student.getStudentName());
            }
        }
        return new Results<>("0", "success", teacher.getTeacherName());
    }
}
